<?php
namespace app\admin666\controller;

use think\Request;
use think\Db;
use think\Loader;
class Member extends Admin
{
    /**
     * 管理员管理首页
     */
    public function index($gid=0,$q=''){
    	$map['id'] = ['gt',1];
    	$map['group_id'] = ['neq',2];
    	if($q != ''){
    		$map['username'] = ['like',"%{$q}%"];
    	}
    	if($gid>0){
    		$map['group_id'] = $gid;
    	}
    	
    	$list = Db::name('member')->field('id,username,name,group_id,status,last_login_ip,last_login_time')
    	->where($map)->paginate(10);
    	$page = $list->render();
    	
    	$_list = $list->toArray();
		$_list = int_to_string($_list['data'],['status'=>['禁用','正常']]);
    	
        $this->assign('_list', $_list);
        $this->assign('_page', $page);
        
        $group = model('AuthGroup')->lists();
        $this->assign('_group', $group);
        return $this->fetch();
    }
	
    /**
    * 添加
    * @date: 2018年6月7日 上午9:48:52
    * @author: xiaocui
    * @param: $pid
    * @return:
    */
    public function add($pid=0){
        if(Request::instance()->isPost()){
            $un = input('post.username','');
            if(!filter_uname($un)){
                $this->error('不允许使用此用户名！');
            }
             
            $m = model('Member');
            
            $data = input('post.');
            $data['create_time'] = NOW_TIME;
            unset($data['id']);
            
            $validate = Loader::validate('Member');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            if($m->allowField(true)->save($data)){
                $uid = $m->id;
                $this->success('添加成功！','');
            }else{
                $this->error($m->getError());
            }
            
        } else {
            $this->assign('_group',model('AuthGroup')->lists());
            return $this->fetch('edit');
        }
    }
    
    /**
     * 修改密码初始化
     */
    public function edit($id = 0){
        empty($id) && $this->error('参数不正确！');
    
        if(Request::instance()->isPost()){
            $un = input('post.username','');
            if(!filter_uname($un)){
                $this->error('不允许使用此用户名！');
            }
             
            $m = model('Member');
            
            $data = input('post.');
            if(empty($data['password'])){
                unset($data['password']);
            }
            unset($data['id']);
            
            if($m->allowField(true)->save($data,['id'=>$id])){
                $this->success('编辑成功！','');
            }else{
                if($m->getError()){
                    $this->error($m->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
            
        }else{
            $info = model('Member')->find($id);
            $this->assign('info',$info);
            $this->assign('_group',model('AuthGroup')->lists());
            return $this->fetch();
        }
    }
    
    
    /**
     * 删除管理员
     */
    public function del(){
        $id = array_unique((array)input('id',0));
    
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map = ['id' => [['neq',1],['in', $id]]];
        if(model('Member')->where($map)->delete()){
            $this->success('删除成功','');
        } else {
            $this->error('删除失败！');
        }
    }
    
    /**
     * 状态
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function status($id,$status){
        $id = intval($id);
        $status = intval($status);
        $model = model('Member');
        if($model->save(['status'=>$status],['id'=>$id])){
            $this->success('修改成功','');
        }else{
            $this->error('修改失败！');
        }
    }
    
}
